package com.jike.noobmoney.mvp.view.activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;

import com.gyf.barlibrary.ImmersionBar;
import com.jike.noobmoney.R;
import com.jike.noobmoney.mvp.view.widget.ProgressDialog;
import com.jike.noobmoney.rxbus.RxBus;
import com.jike.noobmoney.rxbus.RxEvent;
import com.umeng.analytics.MobclickAgent;

import butterknife.ButterKnife;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by kekex on 2018/8/20.
 * MainActivity的基类  RxAppCompatActivity
 */

public abstract class BaseActivity extends AppCompatActivity {

    protected Activity context;
    private Subscription subscribe;
    private ImmersionBar immersionBar;
    private ProgressDialog progressDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = this;
        setContentView(layoutResID());
        ButterKnife.bind(this);
        progressDialog = ProgressDialog.createDialog(this);
        //初始化数据
        initData();
        //状态栏
       // setWindowColor(isfits(), ismain());
        if (isRegistRxBus()) {
            subscribe = RxBus.getInstance().toObservable(RxEvent.class)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(rxEvent -> {
                        if (rxEvent != null) {
                            receiveEvent(rxEvent);
                        }
                    });
            RxBus.getInstance().addSubscription(this, subscribe);
        }
    }

    /**
     * 如果要全屏,重写这个方法返回false
     */
    public Boolean isfits() {
        return false;
    }

    /**
     * 如果要白色主题,重写这个方法返回false
     */
    public boolean ismain() {
        return false;
    }

    /**
     * 设置状态栏颜色
     */
    public void setWindowColor(boolean isfits, boolean ismain) {
        if (ismain) {
            immersionBar = ImmersionBar.with(this)
                    .statusBarColor(R.color.c_main)
                    .statusBarDarkFont(false)
                    .fitsSystemWindows(isfits);
        } else {
            if (isfits) {
                immersionBar = ImmersionBar.with(this)
                        .statusBarColor(R.color.white)
                        .statusBarDarkFont(true)
                        .fitsSystemWindows(true);
            } else {
                immersionBar = ImmersionBar.with(this)
                        .statusBarDarkFont(true)
                        .fitsSystemWindows(false);
            }
        }
        immersionBar.init();
    }

    /**
     * 是否在activity中注册订阅事件
     */
    protected boolean isRegistRxBus() {
        return false;
    }

    /**
     * 注册订阅事件之后重写该方法
     */
    protected void receiveEvent(RxEvent rxEvent) {
    }


    /**
     * 设置布局资源
     */
    protected abstract int layoutResID();

    /**
     * 初始化数据
     */
    protected abstract void initData();

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    protected void showProgress() {
        if (!isFinishing()) {
            if (null != context && null != progressDialog && !isFinishing()) {
                if (!progressDialog.isShowing()) {
                    progressDialog.show();
                }
            }

        }
    }

    protected void dismissProgress() {
        if (!isFinishing()) {
            if (null != context && null != progressDialog) {
                if (progressDialog.isShowing()) {
                    progressDialog.dismiss();
                }
            }
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

}
